package com.cx.common.utils;

import org.apache.commons.lang.math.NumberUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author:cxy
 * @Date:2019/10/24 15:11
 */
public class NumberUtil extends NumberUtils {

    /**
     * 计算总页数
     *
     * @param total 总行数
     * @param size  每页数量
     * @return
     */
    public static Integer getTotalPage(int total, int size) {
        if (total <= 0) {
            return 0;
        }
        if (size <= 0) {
            return 0;
        }
        int totalPage = total / size;
        if (total % size != 0) {
            totalPage++;
        }
        return totalPage;

    }

    /**
     * length1 * wide1 是否在length2 * wide2面积里面
     *
     * @param length1 成品长+出血
     * @param wide1   成品宽+出血
     * @param length2 印刷机最大长
     * @param wide2   印刷机最大宽
     * @return
     */
    public static boolean insideArea(double length1, double wide1, double length2, double wide2) {
        double maxSize = Math.max(length1, wide1);
        double minSize = Math.min(length1, wide1);
        double maxMachineSize = Math.max(length2, wide2);
        double minMachineSize = Math.min(length2, wide2);

        return maxSize <= maxMachineSize && minSize <= minMachineSize;

    }

    public static List<Integer> splitEvenNumber(int pvalue, int splitJointNum) {
        if (splitJointNum < pvalue) {
            return Collections.emptyList();
        }
        if (pvalue <= 0) {
            return Collections.emptyList();
        }
        if (!isEvenNumber(pvalue) || !isEvenNumber(splitJointNum)) {
            return Collections.emptyList();
        }
        List<Integer> list = New.arrayList();
        int total = pvalue;
        int divisor = 0;
        while (total > 0) {
            if (divisor == 0) {
                divisor = splitJointNum / 2;
            } else {
                divisor = divisor / 2;
            }
            if (total - divisor < 0) {
                continue;
            }
            total = total - divisor;
            if (divisor == 0) {
                continue;
            }
            list.add(divisor);
            if (total <= 0) {
                break;
            }
        }
        return list;
    }

    public static List<Integer> findEvenNumbers(int splitJointNum) {
        List<Integer> result = New.arrayList();
        // TODO
        if (splitJointNum % 2 != 0 || splitJointNum < 2) {
            return Collections.emptyList();
        }
        for (int i = 1; i <= splitJointNum / 2; i++) {
            if (splitJointNum % (2 * i) == 0) {
                result.add(i * 2);
            }
        }
        return result;
    }

    public static boolean isEvenNumber(int number) {
        return number % 2 == 0;
    }

    public static boolean isFor4MultipleNum(int number) {
        return number % 4 == 0;
    }

    public static int getInt(Integer number) {
        return number == null ? 0 : number;
    }

    public static boolean equal(Integer number, Integer comparand) {
        return getInt(number) == getInt(comparand);
    }

    /**
     * number是否大于等于comparand
     *
     * @param number
     * @param comparand 被比较数
     * @return
     */
    public static boolean greaterThan(Integer number, Integer comparand) {
        if (number == null) {
            return false;
        }
        if (comparand == null) {
            return true;
        }
        if (number > comparand) {
            return true;
        }
        return false;
    }

    /**
     * 是否大于0
     *
     * @param number
     * @return
     */
    public static boolean isGreaterThanZero(Integer number) {
        if (number == null) {
            return false;
        }
        if (number > 0) {
            return true;
        }
        return false;
    }

    public static boolean isGreaterThanZero(BigDecimal number) {
        if (number == null) {
            return false;
        }
        if (number.compareTo(BigDecimal.ZERO) != -1) {
            return true;
        }
        return false;
    }

    /**
     * @param number
     * @return
     * @Description: 是否大于0
     * @title: isGreaterZero
     * @Author:cxy
     * @Date:2019/10/24 15:11
     * @return: boolean
     */
    public static boolean isGreaterZero(BigDecimal number) {
        if (number == null) {
            return false;
        }
        if (number.compareTo(BigDecimal.ZERO) == 1) {
            return true;
        }
        return false;
    }

    /**
     * @param number
     * @return
     * @Description: 是否大于0
     * @title: isGreaterZero
     * @Author:cxy
     * @Date:2019/10/24 15:11
     * @return: boolean
     */
    public static boolean isGreaterZero(Double number) {
        if (number == null) {
            return false;
        }
        if (number.compareTo(NumberUtils.DOUBLE_ZERO) == 1) {
            return true;
        }
        return false;
    }

    /**
     * @param number
     * @return
     * @Description: 是否大于0
     * @title: isGreaterZero
     * @Author:cxy
     * @Date:2019/10/24 15:11
     * @return: boolean
     */
    public static boolean isGreaterZero(Short number) {
        if (number == null) {
            return false;
        }
        if (number > 0) {
            return true;
        }
        return false;
    }

    /**
     * 是否大于或者等于0
     *
     * @param number
     * @return
     */
    public static boolean isGreaterEqualZero(Integer number) {
        if (number == null) {
            return true;
        }
        if (number >= 0) {
            return true;
        }
        return false;
    }

    /**
     * 是否大于0
     *
     * @param number
     * @return
     */
    public static boolean isGreaterZero(Byte number) {
        return !isLessEqualZero(number);
    }

    /**
     * 是否小于或者等于0
     *
     * @param number
     * @return
     */
    public static boolean isLessEqualZero(Byte number) {
        if (number == null) {
            return true;
        }
        if (number <= 0) {
            return true;
        }
        return false;
    }

    /**
     * 是否小于或者等于0
     *
     * @param number
     * @return
     */
    public static boolean isLessEqualZero(Integer number) {
        if (number == null) {
            return true;
        }
        if (number <= 0) {
            return true;
        }
        return false;
    }

    /***
     * 是否小于等于0
     */
    public static boolean isLessEqualZero(BigDecimal number) {
        if (number == null) {
            return true;
        }
        if (number.compareTo(BigDecimal.ZERO) < 1) {
            return true;
        }
        return false;
    }

    /**
     * 是否小于或者等于0
     *
     * @param number
     * @return
     */
    public static boolean isLessEqualZero(Long number) {
        if (number == null) {
            return true;
        }
        if (number.intValue() <= 0) {
            return true;
        }
        return false;
    }

    public static BigDecimal toBigDecimal(BigDecimal value) {
        return value != null ? value : new BigDecimal(0);
    }

    public static int toInt(Integer value) {
        return value == null ? 0 : value;
    }

    public static int toIntDefault(Integer value, int defaultValue) {
        return value == null ? defaultValue : value;
    }

    public static long toLong(Long value) {
        return value == null ? 0 : value;
    }

    public static long toLongDefault(Long value, long defaultValue) {
        return value == null ? defaultValue : value;
    }

    /***
     * 小于等於0或空则为0，否则返回自身
     *
     * @param price
     * @return
     */
    public static BigDecimal toBigDecimalZero(BigDecimal price) {
        if (price == null || price.compareTo(BigDecimal.ZERO) == -1) {
            return BigDecimal.ZERO;
        }
        return price;
    }

    /**
     * @param @param  bigDecimal
     * @param @return
     * @return BigDecimal
     * @throws @Author:cxy
     * @Description: 四舍五入保留两位小数
     * @Date:2019/10/24 15:11
     */
    public static BigDecimal getRoundDown(BigDecimal bigDecimal) {
        if (bigDecimal == null) {
            bigDecimal = BigDecimal.ZERO;
        }
        return bigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
    }

    @SuppressWarnings("unchecked")
    public static <T> T valueOf(Class<T> resultType, Number value) {
        if (resultType == null) {
            String msg = value.getClass().getSimpleName() + " -> NULL";
            throw new NullPointerException(msg);
        }
        if (resultType == Date.class) {
            return (T) new Date(value.longValue());
        } else if (resultType == int.class || resultType == Integer.class) {
            return (T) Integer.valueOf(value.intValue());
        } else if (resultType == double.class || resultType == Double.class) {
            return (T) value;
        } else if (resultType == boolean.class || resultType == Boolean.class) {
            return (T) Boolean.valueOf(value.intValue() == 0);
        } else if (resultType == byte.class || resultType == Byte.class) {
            return (T) Byte.valueOf(value.byteValue());
        } else if (resultType == long.class || resultType == Long.class) {
            return (T) Long.valueOf(value.longValue());
        } else if (resultType == short.class || resultType == Short.class) {
            return (T) Short.valueOf(value.shortValue());
        } else if (resultType == float.class || resultType == Float.class) {
            return (T) Float.valueOf(value.floatValue());
        } else if (resultType == Number.class) {
            return (T) value;
        } else {
            String msg = value.getClass().getSimpleName() + " -> " + resultType.getSimpleName();
            throw new IllegalArgumentException(new ClassCastException(msg));
        }
    }

    public static String getPaymentsId() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmm");
        String payMentsId = dateFormat.format(new Date()) + (int) (Math.random() * 1000000);
        return payMentsId;
    }

    public static String getOrderId() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmm");
        String orderIdStr = dateFormat.format(new Date()) + (int) (new Random().nextInt(89999) + 10000);
        return orderIdStr;
    }

    /**
     * 生成用户提现记录单号
     *
     * @Param: [userId]
     * @return: java.lang.String
     * @Date:2019/10/24 15:11
     */
    public static String getWithdrawRecordId(Long userId) {
        SimpleDateFormat dateFormat = DateUtils.getDateFormat(DateUtils.PATTERN_SHORT_DATE_TIME);
        String orderIdStr = dateFormat.format(new Date()) + userId + (RandomUtil.nextInt(89999) + 10000);
        return orderIdStr;
    }

    public static String getOrderSn() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmm");
        String orderIdStr = dateFormat.format(new Date()) + (int) (new Random().nextInt(89999) + 10000);
        return orderIdStr;
    }

    public static void main(String[] args) {
        String withdrawRecordId = getWithdrawRecordId(677313L);
        System.out.println(withdrawRecordId);
    }

    public static String getReturnNo() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmm");
        String orderIdStr = dateFormat.format(new Date()) + (int) (new Random().nextInt(89999) + 10000);
        return orderIdStr;
    }

    public static String getRefundOrderSn() {
        return "88" + NumberUtil.getOrderSn();
    }

    public static String getRefundPaymentsId() {
        return "88" + NumberUtil.getPaymentsId();
    }

    /***
     * 获取分
     * @param amount
     * @return
     */
    public static int getCent(BigDecimal amount){
        if(amount == null){
            return 0;
        }
        return amount.multiply(BigDecimal.valueOf(100)).intValue();
    }
}
